//免费馅饼
#include <iostream>
#include <cmath>
#define SECONDS 100001
using namespace std;

long long pies[SECONDS][11] = {0};

int main()
{
    int x, T, i, j, n;
    while (cin >> n)
    {
        if (!n)
        {
            break;
        }
        for(i=0; i<SECONDS; i++)
        {
            for(j=0; j<11; j++)
            {
                pies[i][j]=0;
            }
        }
        while (n--)
        {
            cin >> x >> T;
            pies[T][x] += 1;
        }
        for (i = SECONDS-2; i >= 0; i--)
        {
            pies[i][10] += (pies[i + 1][10] > pies[i + 1][9] ? pies[i + 1][10] : pies[i + 1][9]);
            for (j = 9; j > 0; j--)
            {
                pies[i][j] += max(max(pies[i + 1][j - 1], pies[i + 1][j]), pies[i + 1][j + 1]);
            }
            pies[i][0] += (pies[i + 1][0] > pies[i + 1][1] ? pies[i + 1][0] : pies[i + 1][1]);
        }
        cout << pies[0][5] << endl;
    }
    return 0;
}